package com.etd.base.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.etd.base.config.ConstSysParam;
import com.etd.base.domain.inf.BaseDomainInf;
public class LongUtil {
	
	public LongUtil(){};
	
	public static Long getLongValue(String longVal,Long defaultVal){
		
		Long _longVal = defaultVal;
		
		if(longVal!=null){ 
			try{
				_longVal = Long.parseLong(longVal);
			}catch(Exception e){
				System.out.println(" Execption for parse Long value -------");
			}			
		}
		
		return _longVal;
	}
	
	public static Integer getIntValue(String longVal,Integer defaultVal){
		
		Integer _longVal = defaultVal;
		
		if(longVal!=null){ 
			try{
				_longVal = Integer.parseInt(longVal);
			}catch(Exception e){
				System.out.println(" Execption for parse Long value -------");
			}
			
		}
		
		return _longVal;
	}
	
	/**
	 * 将字符串集合转化为Long数据集合
	 * @param strList
	 * @return
	 */
	public static List<Long> getLongList(List<String> strList){
		
		List<Long> longList = new ArrayList<Long>();
		if((strList!=null) && (strList.size()>0)){
			for(int i=0;i<strList.size();i++){
				longList.add(Long.parseLong(strList.get(i)));
			}
		}
		return longList;
	}
	
	/**
	 * 获取实体类id集合
	 * @param strList
	 * @return List<Long>
	 */
	public static List<Long> getLongIdList(List<? extends BaseDomainInf> domains){
		
		List<Long> longList = new ArrayList<Long>();
		if((domains!=null) && (domains.size()>0)){
			for(int i=0;i<domains.size();i++){
				longList.add((domains.get(i).getId()));
			}
		}
		return longList;
	}

	/**
	 * 获取分页参数
	 * @param conditionMap
	 * @return
	 */
	public static Integer[] getPageParam(Map<String,Object> conditionMap){
		
		Integer[] param = new Integer[2];
		param[0] = ConstSysParam.PAGE;
		param[1] = ConstSysParam.ROWS;
		
		//获取页号
		Object _pageObj = conditionMap.containsKey("page")?conditionMap.get("page"):
			(conditionMap.containsKey("page_num")?conditionMap.get("page_num"):null);
		if(_pageObj!=null){
			param[0] = LongUtil.getIntValue(_pageObj.toString(),null);
		}
		
		//获取每页行数
		Object _rowsObj = conditionMap.containsKey("rows")?conditionMap.get("rows"):
				(conditionMap.containsKey("rows_per_page")?conditionMap.get("rows_per_page"):
					(conditionMap.containsKey("length")?conditionMap.get("length"):null));
		
		if(_rowsObj!=null){
			param[1] = LongUtil.getIntValue(_rowsObj.toString(),null);
		}	
		
		if(conditionMap.containsKey("start")){
			Object obj = conditionMap.get("start");
			if(obj!=null){
				Integer _p = LongUtil.getIntValue(obj.toString(), null);
				if(_p!=null){
					param[0] = (_p /param[1])+1;
				}
			}
		}
		
		return param;
	}
}
